local VoteVGUI = {}
local QuestionVGUI = {}
local PanelNum = 0
local LetterWritePanel

local function MsgDoVote(msg)
	local question = msg:ReadString()
	local voteid = msg:ReadString()
	local timeleft = msg:ReadFloat()
	if timeleft == 0 then
		timeleft = 100
	end
	local OldTime = CurTime()
	if string.find(voteid, LocalPlayer():EntIndex()) then return end --If it's about you then go away

	LocalPlayer():EmitSound("Town.d1_town_02_elevbell1", 100, 100)
	local panel = vgui.Create("DFrame")
	panel:SetPos(3 + PanelNum, ScrH() / 2 - 50)
	panel:SetTitle("Vote")
	panel:SetSize(140, 140)
	panel:SetSizable(false)
	panel.btnClose:SetVisible(false)
	panel:SetDraggable(false)
	function panel:Close()
		PanelNum = PanelNum - 140
		VoteVGUI[voteid .. "vote"] = nil
		
		local num = 0
		for k,v in SortedPairs(VoteVGUI) do
			v:SetPos(num, ScrH() / 2 - 50)
			num = num + 140
		end
		
		for k,v in SortedPairs(QuestionVGUI) do
			v:SetPos(num, ScrH() / 2 - 50)
			num = num + 300
		end
		self:Remove()
	end
	
	function panel:Think()
		self:SetTitle("Time: ".. tostring(math.Clamp(math.ceil(timeleft - (CurTime() - OldTime)), 0, 9999)))
		if timeleft - (CurTime() - OldTime) <= 0 then 
			panel:Close()
		end
	end

	panel:SetKeyboardInputEnabled(false)
	panel:SetMouseInputEnabled(true)
	panel:SetVisible(true)

	local label = vgui.Create("Label")
	label:SetParent(panel)
	label:SetPos(5, 30)
	label:SetSize(180, 40)
	label:SetText(question)
	label:SetVisible(true)

	local divider = vgui.Create("Divider")
	divider:SetParent(panel)
	divider:SetPos(2, 80)
	divider:SetSize(180, 2)
	divider:SetVisible(true)

	local ybutton = vgui.Create("Button")
	ybutton:SetParent(panel)
	ybutton:SetPos(25, 100)
	ybutton:SetSize(40, 20)
	ybutton:SetCommand("!")
	ybutton:SetText("Yes")
	ybutton:SetVisible(true)
	ybutton.DoClick = function()
		LocalPlayer():ConCommand("vote " .. voteid .. " 1\n")
		panel:Close()
	end

	local nbutton = vgui.Create("Button")
	nbutton:SetParent(panel)
	nbutton:SetPos(70, 100)
	nbutton:SetSize(40, 20)
	nbutton:SetCommand("!")
	nbutton:SetText("No")
	nbutton:SetVisible(true)
	nbutton.DoClick = function()
		LocalPlayer():ConCommand("vote " .. voteid .. " 2\n")
		panel:Close()
	end

	PanelNum = PanelNum + 140
	VoteVGUI[voteid .. "vote"] = panel
	panel:SetSkin("DarkRP")
end
usermessage.Hook("DoVote", MsgDoVote)

local function KillVoteVGUI(msg)
	local id = msg:ReadString()
	
	if VoteVGUI[id .. "vote"] and VoteVGUI[id .. "vote"]:IsValid() then
		VoteVGUI[id.."vote"]:Close()

	end
end
usermessage.Hook("KillVoteVGUI", KillVoteVGUI)

local function MsgDoQuestion(msg)
	local question = msg:ReadString()
	local quesid = msg:ReadString()
	local timeleft = msg:ReadFloat()
	if timeleft == 0 then
		timeleft = 100
	end
	local OldTime = CurTime()
	LocalPlayer():EmitSound("Town.d1_town_02_elevbell1", 100, 100)
	local panel = vgui.Create("DFrame")
	panel:SetPos(3 + PanelNum, ScrH() / 2 - 50)--Times 140 because if the quesion is the second screen, the first screen is always a vote screen.
	panel:SetSize(300, 140)
	panel:SetSizable(false)
	panel.btnClose:SetVisible(false)
	panel:SetKeyboardInputEnabled(false)
	panel:SetMouseInputEnabled(true)
	panel:SetVisible(true)
	
	function panel:Close()
		PanelNum = PanelNum - 300
		QuestionVGUI[quesid .. "ques"] = nil
		local num = 0
		for k,v in SortedPairs(VoteVGUI) do
			v:SetPos(num, ScrH() / 2 - 50)
			num = num + 140
		end
		
		for k,v in SortedPairs(QuestionVGUI) do
			v:SetPos(num, ScrH() / 2 - 50)
			num = num + 300
		end
		
		self:Remove()
	end
	
	function panel:Think()
		self:SetTitle("Time: ".. tostring(math.Clamp(math.ceil(timeleft - (CurTime() - OldTime)), 0, 9999)))
		if timeleft - (CurTime() - OldTime) <= 0 then 
			panel:Close()
		end
	end

	local label = vgui.Create("DLabel")
	label:SetParent(panel)
	label:SetPos(5, 30)
	label:SetSize(380, 40)
	label:SetText(question)
	label:SetVisible(true)

	local divider = vgui.Create("Divider")
	divider:SetParent(panel)
	divider:SetPos(2, 80)
	divider:SetSize(380, 2)
	divider:SetVisible(true)

	local ybutton = vgui.Create("DButton")
	ybutton:SetParent(panel)
	ybutton:SetPos(/*147*/105, 100)
	ybutton:SetSize(40, 20)
	ybutton:SetText("Yes")
	ybutton:SetVisible(true)
	ybutton.DoClick = function()
		LocalPlayer():ConCommand("ans " .. quesid .. " 1\n")
		panel:Close()
	end
	
	local nbutton = vgui.Create("DButton")
	nbutton:SetParent(panel)
	nbutton:SetPos(155, 100)
	nbutton:SetSize(40, 20)
	nbutton:SetText("No")
	nbutton:SetVisible(true)
	nbutton.DoClick = function()
		LocalPlayer():ConCommand("ans " .. quesid .. " 2\n")
		panel:Close()
	end

	PanelNum = PanelNum + 300
	QuestionVGUI[quesid .. "ques"] = panel
	
	panel:SetSkin("DarkRP")
end
usermessage.Hook("DoQuestion", MsgDoQuestion)

local function KillQuestionVGUI(msg)
	local id = msg:ReadString()

	if QuestionVGUI[id .. "ques"] and QuestionVGUI[id .. "ques"]:IsValid() then
		QuestionVGUI[id .. "ques"]:Close()
	end
end
usermessage.Hook("KillQuestionVGUI", KillQuestionVGUI)

local function DoVoteAnswerQuestion(ply, cmd, args)
	if not args[1] then return end
	
	local vote = 2
	if tonumber(args[1]) == 1 or string.lower(args[1]) == "yes" or string.lower(args[1]) == "true" then vote = 1 end
	
	for k,v in pairs(VoteVGUI) do
		if ValidPanel(v) then
			local ID = string.sub(k, 1, -5)
			VoteVGUI[k]:Close()
			RunConsoleCommand("vote", ID, vote)
			return
		end
	end
	
	for k,v in pairs(QuestionVGUI) do
		if ValidPanel(v) then
			local ID = string.sub(k, 1, -5)
			QuestionVGUI[k]:Close()
			RunConsoleCommand("ans", ID, vote)
			return
		end
	end
end
concommand.Add("rp_vote", DoVoteAnswerQuestion)

local function DoLetter(msg)
	LetterWritePanel = vgui.Create("Frame")
	LetterWritePanel:SetPos(ScrW() / 2 - 75, ScrH() / 2 - 100)
	LetterWritePanel:SetSize(150, 200)
	LetterWritePanel:SetMouseInputEnabled(true)
	LetterWritePanel:SetKeyboardInputEnabled(true)
	LetterWritePanel:SetVisible(true)
end
usermessage.Hook("DoLetter", DoLetter)

local DRP = {}

local function MayorOptns()
	local MayCat = vgui.Create("DCollapsibleCategory")
	function MayCat:Paint()
		self:SetBGColor(team.GetColor(LocalPlayer():Team()))
	end
	MayCat:SetLabel("Mayor options")
		local maypanel = vgui.Create("DPanelList")
		maypanel:SetSpacing(5)
		maypanel:SetSize(740,170)
		maypanel:EnableHorizontal(false)
		maypanel:EnableVerticalScrollbar(true)
			local SearchWarrant = vgui.Create("DButton") 
			SearchWarrant:SetText(LANGUAGE.give_money)
			SearchWarrant.DoClick = function()
				local menu = DermaMenu()
				for _,ply in pairs(player.GetAll()) do
					if not ply.DarkRPVars.warrant and ply ~= LocalPlayer() then
						menu:AddOption(ply:Nick(), function() 
							Derma_StringRequest("Warrant", "Why would you warrant "..ply:Nick().."?", nil, 
								function(a) 
								LocalPlayer():ConCommand("say /warrant ".. tostring(ply:UserID()).." ".. a)
								end, function() end ) end)
					end
				end
				if #menu.Items == 0 then
					menu:AddOption(LANGUAGE.noone_available, function() end)
				end
				menu:Open()
			end
			maypanel:AddItem(SearchWarrant)
			
			local Warrant = vgui.Create("DButton") 
			Warrant:SetText(LANGUAGE.make_wanted)
			Warrant.DoClick = function()
				local menu = DermaMenu()
				for _,ply in pairs(player.GetAll()) do
					if not ply.DarkRPVars.wanted and ply ~= LocalPlayer() then
						menu:AddOption(ply:Nick(), function() Derma_StringRequest("wanted", "Why would you make "..ply:Nick().." wanted?", nil, 
								function(a) 
								LocalPlayer():ConCommand("say /wanted ".. tostring(ply:UserID()).." ".. a)
								end, function() end ) end)
					end
				end
				if #menu.Items == 0 then
					menu:AddOption("Noone available", function() end)
				end
				menu:Open()
			end
			maypanel:AddItem(Warrant)
			
			local UnWarrant = vgui.Create("DButton") 
			UnWarrant:SetText(LANGUAGE.make_unwanted)
			UnWarrant.DoClick = function()
				local menu = DermaMenu()
				for _,ply in pairs(player.GetAll()) do
					if ply.DarkRPVars.wanted and ply ~= LocalPlayer() then
						menu:AddOption(ply:Nick(), function() LocalPlayer():ConCommand("say /unwanted " .. tostring(ply:UserID())) end)
					end
				end
				if #menu.Items == 0 then
					menu:AddOption(LANGUAGE.noone_available, function() end)
				end
				menu:Open()
			end
			maypanel:AddItem(UnWarrant)
			
			local Lockdown = vgui.Create("DButton") 
			Lockdown:SetText(LANGUAGE.initiate_lockdown)
			Lockdown.DoClick = function()
				LocalPlayer():ConCommand("say /lockdown")
			end
			maypanel:AddItem(Lockdown)
			
			
			local UnLockdown = vgui.Create("DButton") 
			UnLockdown:SetText(LANGUAGE.stop_lockdown)
			UnLockdown.DoClick = function()
				LocalPlayer():ConCommand("say /unlockdown")
			end
			maypanel:AddItem(UnLockdown)
			
			local Lottery = vgui.Create("DButton") 
			Lottery:SetText(LANGUAGE.start_lottery)
			Lottery.DoClick = function()
				LocalPlayer():ConCommand("say /lottery")
			end
			maypanel:AddItem(Lottery)
			
			local GiveLicense = vgui.Create("DButton") 
			GiveLicense:SetText(LANGUAGE.give_license_lookingat)
			GiveLicense.DoClick = function()
				LocalPlayer():ConCommand("say /givelicense")
			end
			maypanel:AddItem(GiveLicense)

			local Proplympics = vgui.Create("DButton") 
			Proplympics:SetText("Organize proplympics!")
			Proplympics.DoClick = function()
				LocalPlayer():ConCommand("say /proplympics")
			end
			maypanel:AddItem(Proplympics)
			
			local PlaceLaws = vgui.Create("DButton")
			PlaceLaws:SetText("Place a screen containing the laws.")
			PlaceLaws.DoClick = function()
				LocalPlayer():ConCommand("say /placelaws")
			end
			maypanel:AddItem(PlaceLaws)
			
			local AddLaws = vgui.Create("DButton")
			AddLaws:SetText("Add a law.")
			AddLaws.DoClick = function()
				Derma_StringRequest("Add a law", "Type the law you would like to add here.", "", function(law)
					LocalPlayer():ConCommand("say /addlaw " .. law)
				end)
			end
			maypanel:AddItem(AddLaws)
			
			local RemLaws = vgui.Create("DButton")
			RemLaws:SetText("Remove a law.")
			RemLaws.DoClick = function()
				Derma_StringRequest("Remove a law", "Enter the number of the law you would like to remove here.", "", function(num)
					LocalPlayer():ConCommand("say /removelaw " .. num)
				end)
			end
			maypanel:AddItem(RemLaws)
	MayCat:SetContents(maypanel)
	MayCat:SetSkin("DarkRP")
	return MayCat
end

local function CPOptns()
	local CPCat = vgui.Create("DCollapsibleCategory")
	function CPCat:Paint()
		self:SetBGColor(team.GetColor(LocalPlayer():Team()))
	end
	CPCat:SetLabel("Police options")
		local CPpanel = vgui.Create("DPanelList")
		CPpanel:SetSpacing(5)
		CPpanel:SetSize(740,170)
		CPpanel:EnableHorizontal(false)
		CPpanel:EnableVerticalScrollbar(true)
			local SearchWarrant = vgui.Create("DButton") 
			SearchWarrant:SetText(LANGUAGE.request_warrant)
			SearchWarrant.DoClick = function()
				local menu = DermaMenu()
				for _,ply in pairs(player.GetAll()) do
					if not ply.DarkRPVars.warrant and ply ~= LocalPlayer() then
						menu:AddOption(ply:Nick(), function() 
							Derma_StringRequest("Warrant", "Why would you warrant "..ply:Nick().."?", nil, 
								function(a) 
								LocalPlayer():ConCommand("say /warrant ".. tostring(ply:UserID()).." ".. a)
								end, function() end ) end)
					end
				end
				if #menu.Items == 0 then
					menu:AddOption(LANGUAGE.noone_available, function() end) 
				end
				menu:Open()
			end
			CPpanel:AddItem(SearchWarrant)
			
			local Warrant = vgui.Create("DButton") 
			Warrant:SetText(LANGUAGE.searchwarrantbutton)
			Warrant.DoClick = function()
				local menu = DermaMenu()
				for _,ply in pairs(player.GetAll()) do
					if not ply.DarkRPVars.wanted and ply ~= LocalPlayer() then
						menu:AddOption(ply:Nick(), function() Derma_StringRequest("wanted", "Why would you make "..ply:Nick().." wanted?", nil, 
								function(a) 
								LocalPlayer():ConCommand("say /wanted ".. tostring(ply:UserID()).." ".. a)
								end, function() end ) end)
					end
				end
				if #menu.Items == 0 then
					menu:AddOption(LANGUAGE.noone_available, function() end)
				end
				menu:Open()
			end
			CPpanel:AddItem(Warrant)
			
			local UnWarrant = vgui.Create("DButton") 
			UnWarrant:SetText(LANGUAGE.unwarrantbutton)
			UnWarrant.DoClick = function()
				local menu = DermaMenu()
				for _,ply in pairs(player.GetAll()) do
					if ply.DarkRPVars.wanted and ply ~= LocalPlayer() then
						menu:AddOption(ply:Nick(), function() LocalPlayer():ConCommand("say /unwanted " .. tostring(ply:UserID())) end)
					end
				end
				if #menu.Items == 0 then
					menu:AddOption(LANGUAGE.noone_available, function() end)
				end
				menu:Open()
			end
			CPpanel:AddItem(UnWarrant)
			
			if LocalPlayer():Team() == TEAM_CHIEF or LocalPlayer():IsAdmin() then
				local SetJailPos = vgui.Create("DButton") 
				SetJailPos:SetText(LANGUAGE.set_jailpos)
				SetJailPos.DoClick = function() LocalPlayer():ConCommand("say /jailpos") end
				CPpanel:AddItem(SetJailPos)
				
				local AddJailPos = vgui.Create("DButton") 
				AddJailPos:SetText(LANGUAGE.add_jailpos)
				AddJailPos.DoClick = function() LocalPlayer():ConCommand("say /addjailpos") end
				CPpanel:AddItem(AddJailPos)
			end
			
			local ismayor -- Firstly look if there's a mayor
			local ischief -- Then if there's a chief
			for k,v in pairs(player.GetAll()) do
				if v:Team() == TEAM_MAYOR then
					ismayor = true
					break
				end
			end
			
			if not ismayor then
				for k,v in pairs(player.GetAll()) do
					if v:Team() == TEAM_CHIEF then
						ischief = true
						break
					end
				end
			end
			
			local Team = LocalPlayer():Team()
			if not ismayor and (Team == TEAM_CHIEF or (not ischief and Team == TEAM_POLICE)) then
				local GiveLicense = vgui.Create("DButton") 
				GiveLicense:SetText(LANGUAGE.give_license_lookingat)
				GiveLicense.DoClick = function()
					LocalPlayer():ConCommand("say /givelicense")
				end
				CPpanel:AddItem(GiveLicense)
			end
	CPCat:SetContents(CPpanel)
	CPCat:SetSkin("DarkRP")
	return CPCat
end


local function CitOptns()
	local CitCat = vgui.Create("DCollapsibleCategory")
	function CitCat:Paint()
		self:SetBGColor(team.GetColor(LocalPlayer():Team()))
	end
	CitCat:SetLabel("Citizen options")
		local Citpanel = vgui.Create("DPanelList")
		Citpanel:SetSpacing(5)
		Citpanel:SetSize(740,110)
		Citpanel:EnableHorizontal(false)
		Citpanel:EnableVerticalScrollbar(true)
		
		local joblabel = vgui.Create("DLabel")
		joblabel:SetText(LANGUAGE.set_custom_job)
		Citpanel:AddItem(joblabel)
		
		local jobentry = vgui.Create("DTextEntry")
		jobentry:SetValue(LocalPlayer().DarkRPVars.job or "")
		jobentry.OnEnter = function()
			LocalPlayer():ConCommand("say /job " .. tostring(jobentry:GetValue()))
		end
		jobentry.OnLoseFocus = jobentry.OnEnter
		Citpanel:AddItem(jobentry)
		
	CitCat:SetContents(Citpanel)
	CitCat:SetSkin("DarkRP")
	return CitCat
end


local function MobOptns()
	local MobCat = vgui.Create("DCollapsibleCategory")
	function MobCat:Paint()
		self:SetBGColor(team.GetColor(LocalPlayer():Team()))
	end
	MobCat:SetLabel("Mobboss options")
		local Mobpanel = vgui.Create("DPanelList")
		Mobpanel:SetSpacing(5)
		Mobpanel:SetSize(740,110)
		Mobpanel:EnableHorizontal(false)
		Mobpanel:EnableVerticalScrollbar(true)
		
		local agendalabel = vgui.Create("DLabel")
		agendalabel:SetText(LANGUAGE.set_agenda)
		Mobpanel:AddItem(agendalabel)
		
		local agendaentry = vgui.Create("DTextEntry")
		agendaentry:SetValue(LocalPlayer().DarkRPVars.agenda or "")
		agendaentry.OnEnter = function()
			LocalPlayer():ConCommand("say /agenda " .. tostring(agendaentry:GetValue()))
		end
		agendaentry.OnLoseFocus = agendaentry.OnEnter
		Mobpanel:AddItem(agendaentry)
		
	MobCat:SetContents(Mobpanel)
	MobCat:SetSkin("DarkRP")
	return MobCat
end

DRP.Tabs = {}
function DRP.AddTab(Name,Derma)
	DRP.Tabs[Name] = {}
	DRP.Tabs[Name].derma = Derma
end

DRP.AddTab("Commands",function(p)
		local FirstTabPanel = vgui.Create("DPanelList",p)
	FirstTabPanel:EnableVerticalScrollbar( true )
		function FirstTabPanel:Update()
			self:SetPos(ScrW()*.005,ScrH()*.007)
			self:SetSize(ScrW()*.69,ScrH()*.785)
			self:Clear(true)
			local MoneyCat = vgui.Create("DCollapsibleCategory")
			MoneyCat:SetLabel("Money")
				local MoneyPanel = vgui.Create("DPanelList")
				MoneyPanel:SetSpacing(5)
				MoneyPanel:SetSize(740,60)
				MoneyPanel:EnableHorizontal(false)
				MoneyPanel:EnableVerticalScrollbar(true)
				
				local GiveMoneyButton = vgui.Create("DButton")
				GiveMoneyButton:SetText(LANGUAGE.give_money)
				GiveMoneyButton.DoClick = function()
					Derma_StringRequest("Amount of money", "How much money do you want to give?", "", function(a) LocalPlayer():ConCommand("say /give " .. tostring(a)) end)
				end
				MoneyPanel:AddItem(GiveMoneyButton)
				local SpawnMoneyButton = vgui.Create("DButton")
				SpawnMoneyButton:SetText(LANGUAGE.drop_money)
				SpawnMoneyButton.DoClick = function()
					Derma_StringRequest("Amount of money", "How much money do you want to drop?", "", function(a) LocalPlayer():ConCommand("say /dropmoney " .. tostring(a)) end)
				end

				MoneyPanel:AddItem(SpawnMoneyButton)
			MoneyCat:SetContents(MoneyPanel)
			MoneyCat:SetSkin("DarkRP")
		
		
			local Commands = vgui.Create("DCollapsibleCategory")
			Commands:SetLabel("Actions")
				local ActionsPanel = vgui.Create("DPanelList")
				ActionsPanel:SetSpacing(5)
				ActionsPanel:SetSize(740,210)
				ActionsPanel:EnableHorizontal( false )
				ActionsPanel:EnableVerticalScrollbar(true)
					local rpnamelabel = vgui.Create("DLabel")
					rpnamelabel:SetText(LANGUAGE.change_name)
				ActionsPanel:AddItem(rpnamelabel) 
					
					local rpnameTextbox = vgui.Create("DTextEntry")
					rpnameTextbox:SetText(LocalPlayer():Nick())
					rpnameTextbox.OnEnter = function() LocalPlayer():ConCommand("say /rpname " .. tostring(rpnameTextbox:GetValue())) end
					rpnameTextbox.OnLoseFocus = function() LocalPlayer():ConCommand("say /rpname " .. tostring(rpnameTextbox:GetValue())) end
					
					ActionsPanel:AddItem(rpnameTextbox)
				
					local sleep = vgui.Create("DButton")
					sleep:SetText(LANGUAGE.go_to_sleep)
					sleep.DoClick = function()
						LocalPlayer():ConCommand("say /sleep")
					end	
				ActionsPanel:AddItem(sleep)
					local Drop = vgui.Create("DButton")
					Drop:SetText(LANGUAGE.drop_weapon)
					Drop.DoClick = function() LocalPlayer():ConCommand("say /drop") end
				ActionsPanel:AddItem(Drop)
					local health = vgui.Create("DButton")
					health:SetText(string.format(LANGUAGE.buy_health, tostring(GetConVarNumber("healthcost"))))
					health.DoClick = function() LocalPlayer():ConCommand("say /Buyhealth") end
				ActionsPanel:AddItem(health)
				
				if LocalPlayer():Team() ~= TEAM_MAYOR then
					local RequestLicense = vgui.Create("DButton")
						RequestLicense:SetText(LANGUAGE.request_gunlicense)
						RequestLicense.DoClick = function() LocalPlayer():ConCommand("say /requestlicense") end
					ActionsPanel:AddItem(RequestLicense)
				end
				
				local Demote = vgui.Create("DButton") 
				Demote:SetText(LANGUAGE.demote_player_menu)
				Demote.DoClick = function()
					local menu = DermaMenu()
					for _,ply in pairs(player.GetAll()) do
						if ply ~= LocalPlayer() then
							menu:AddOption(ply:Nick(), function()
								Derma_StringRequest("Demote reason", "Why would you demote "..ply:Nick().."?", nil, 
									function(a) 
									LocalPlayer():ConCommand("say /demote ".. tostring(ply:UserID()).." ".. a)
									end, function() end )
							end)
						end
					end
					if #menu.Items == 0 then
						menu:AddOption(LANGUAGE.noone_available, function() end) 
					end
					menu:Open()
				end
				ActionsPanel:AddItem(Demote)
				
				local UnOwnAllDoors = vgui.Create("DButton")
						UnOwnAllDoors:SetText("Sell all of your doors")
						UnOwnAllDoors.DoClick = function() LocalPlayer():ConCommand("say /unownalldoors") end
					ActionsPanel:AddItem(UnOwnAllDoors)
			Commands:SetContents(ActionsPanel)
		FirstTabPanel:AddItem(MoneyCat)
		Commands:SetSkin("DarkRP")
		FirstTabPanel:AddItem(Commands)
		
		if LocalPlayer():Team() == TEAM_MAYOR then
			FirstTabPanel:AddItem(MayorOptns())
		elseif LocalPlayer():Team() == TEAM_CITIZEN then
			FirstTabPanel:AddItem(CitOptns())
		elseif LocalPlayer():Team() == TEAM_POLICE or LocalPlayer():Team() == TEAM_CHIEF then
			FirstTabPanel:AddItem(CPOptns())
		elseif LocalPlayer():Team() == TEAM_MOB then
			FirstTabPanel:AddItem(MobOptns())
		end
	end
	FirstTabPanel:Update()
end)


local SDB_JobInfo = SDB_TeamNameToInfo
DRP.AddTab("Jobs",function(p)
	local hordiv = vgui.Create("DHorizontalDivider",p)
	hordiv:SetLeftWidth(370)
	hordiv:SetSize(ScrW()*.69,ScrH()*.785)
	hordiv:SetPos(ScrW()*.005,ScrH()*.007)
	function hordiv.m_DragBar:OnMousePressed() end
	hordiv.m_DragBar:SetCursor("none")
	local Panel
	local Information
	function hordiv:Update()
		if Panel and Panel:IsValid() then
			Panel:Remove()
		end
		Panel = vgui.Create( "DPanelList")
		Panel:SetSize(370, 540)
		Panel:SetSpacing(1)
		Panel:EnableHorizontal( true )
		Panel:EnableVerticalScrollbar( true )
		Panel:SetSkin("DarkRP")

		
		local Info = {}
		local model
		local modelpanel
		local function UpdateInfo(a)
			if Information and Information:IsValid() then
				Information:Remove()
			end
			Information = vgui.Create( "DPanelList" )
			Information:SetPadding(8)
			Information:SetSpacing(8)
			Information:SetPos(378,0)
			Information:SetSize(370, 540)
			Information:SetSpacing(10)
			Information:EnableHorizontal( false )
			Information:SetSkin("DarkRP")
			Information:SetDrawBackground(false)
			function Information:Rebuild() -- YES IM OVERRIDING IT AND CHANGING ONLY ONE LINE BUT I HAVE A FUCKING GOOD REASON TO DO IT!
				local Offset = 0
				if ( self.Horizontal ) then
					local x, y = self.Padding, self.Padding;
					for k, panel in pairs( self.Items ) do
						local w = panel:GetWide()
						local h = panel:GetTall()
						if ( x + w  > self:GetWide() ) then
							x = self.Padding
							y = y + h + self.Spacing
						end
						panel:SetPos( x, y )
						x = x + w + self.Spacing
						Offset = y + h + self.Spacing
					end
				else
					for k, panel in pairs( self.Items ) do
						if not panel:IsValid() then return end
						panel:SetSize( self:GetCanvas():GetWide() - self.Padding * 2, panel:GetTall() )
						panel:SetPos( self.Padding, self.Padding + Offset )
						panel:InvalidateLayout( true )
						Offset = Offset + panel:GetTall() + self.Spacing
					end
					Offset = Offset + self.Padding	
				end
				self:GetCanvas():SetTall( Offset + (self.Padding) - self.Spacing ) 
			end
			
			if type(Info) == "table" and #Info > 0 then
				for k,v in ipairs(Info) do
					local label = vgui.Create("DLabel")
					label:SetFont("Trebuchet22")
					label:SetText(v)
					label:SizeToContents()
					if label:IsValid() then
						Information:AddItem(label)
					end
				end
			end

			if model and type(model) == "string" and a ~= false then
				modelpanel = vgui.Create("DModelPanel")
				modelpanel:SetModel(model)
				modelpanel:SetSize(90,230)
				modelpanel:SetAnimated(true)
				modelpanel:SetFOV(90)
				modelpanel:SetAnimSpeed(1)
				if modelpanel:IsValid() then
					Information:AddItem(modelpanel)
				end
			end
			hordiv:SetLeft(Panel)
			hordiv:SetRight(Information)
		end
		UpdateInfo()
		Information:EnableVerticalScrollbar( false )
		
		local function AddIcon(Model, name, description, Weapons, command, special, specialcommand)
			local icon = vgui.Create("SpawnIcon")
			
			local IconModel = Model
			if type(Model) == "table" then
				IconModel = Model[math.random(#Model)]
			end
			icon:SetModel(IconModel)
			
			local paintname
			local painttxt
			local tlen = string.len(name)
			if tlen > 12 then
				painttxt = "Trebuchet22"
				paintname = (tlen > 15 and (string.sub(name,1,12) .. "..")) or name
			else
				paintname = name
				painttxt = "Trebuchet24"
			end
			
			local jobinf = SDB_JobInfo[name]
			local clr = jobinf.clr
			icon.PaintOver = function()
				draw.RoundedBox(6, 2, 2, 116, 30, Color(0,0,0,200))
				draw.SimpleTextOutlined(paintname, painttxt, 59, 15, Color(clr.r+40,clr.g+40,clr.b+40), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2, Color(0,0,0,255))
			end
			
			local saltxt, maxtxt = (jobinf.sal > 0 and ("Salary: $" .. jobinf.sal) or "No Salary"), "Max: " .. jobinf.max
			icon.PaintOverHovered = function()
				draw.RoundedBox(6, 2, 2, 116, 60, Color(0,0,0,200))
				draw.SimpleTextOutlined(saltxt, "Trebuchet22", 58, 15, Color(0,188,31), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2, Color(0,0,0,255))
				draw.SimpleTextOutlined(maxtxt, "Trebuchet22", 58, 45, Color(0,188,31), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2, Color(0,0,0,255))
			end
			
			icon:SetIconSize(120)
			icon:SetToolTip()
			icon.OnCursorEntered = function()
				icon.PaintOverOld = icon.PaintOver 
				icon.PaintOver = icon.PaintOverHovered
				Info[1] = LANGUAGE.job_name .. name 
				Info[2] = LANGUAGE.job_description .. description
				Info[3] = LANGUAGE.job_weapons .. Weapons
				Info[4] = saltxt .. "\n\n" .. maxtxt
				model = IconModel
				UpdateInfo()
			end
			icon.OnCursorExited = function()
				if ( icon.PaintOver == icon.PaintOverHovered ) then 
					icon.PaintOver = icon.PaintOverOld 
				end
				Info = {}
				if modelpanel and modelpanel:IsValid() and icon:IsValid() then
					modelpanel:Remove()
					UpdateInfo(false)
				end
			end
			
			icon.DoClick = function()
				local function DoChatCommand(frame)
					if special then
						local menu = DermaMenu()
						menu:AddOption("Vote", function() LocalPlayer():ConCommand("say "..command) frame:Close() end)
						menu:AddOption("Do not vote", function() LocalPlayer():ConCommand("say " .. specialcommand) frame:Close() end)
						menu:Open()
					else
						LocalPlayer():ConCommand("say " .. command)
						frame:Close()
					end
				end
				
				if type(Model) == "table" and #Model > 0 then
					hordiv:GetParent():GetParent():Close()
					local frame = vgui.Create( "DFrame" )
					frame:SetTitle( "Choose model" )
					frame:SetVisible(true)
					frame:MakePopup()
					
					local levels = 1
					local IconsPerLevel = math.floor(ScrW()/64)
					
					while #Model * (64/levels) > ScrW() do
						levels = levels + 1
					end
					frame:SetSize(math.Min(#Model * 64, IconsPerLevel*64), math.Min(90+(64*(levels-1)), ScrH()))
					frame:Center()
					
					local CurLevel = 1
					for k,v in pairs(Model) do
						local icon = vgui.Create("SpawnIcon", frame)
						if (k-IconsPerLevel*(CurLevel-1)) > IconsPerLevel then
							CurLevel = CurLevel + 1
						end
						icon:SetPos((k-1-(CurLevel-1)*IconsPerLevel) * 64, 25+(64*(CurLevel-1)))
						icon:SetModel(v)
						icon:SetIconSize(64)
						icon:SetToolTip()
						icon.DoClick = function()
							RunConsoleCommand("rp_playermodel", v)
							RunConsoleCommand("_rp_ChosenModel", v)
							DoChatCommand(frame)
						end
					end
				else
					DoChatCommand(hordiv:GetParent():GetParent())
				end
			end
			
			if icon:IsValid() then
				Panel:AddItem(icon)
			end
		end
		
		for k,v in ipairs(RPExtraTeams) do
			if LocalPlayer():Team() ~= k then
				local nodude = true
				if v.admin == 1 and not LocalPlayer():IsAdmin() then
					nodude = false
				end
				if v.admin > 1 and not LocalPlayer():IsSuperAdmin() then
					nodude = false
				end
				if v.customCheck and not v.customCheck(LocalPlayer()) then
					nodude = false
				end
				
				if (type(v.NeedToChangeFrom) == "number" and LocalPlayer():Team() ~= v.NeedToChangeFrom) or (type(v.NeedToChangeFrom) == "table" and not table.HasValue(v.NeedToChangeFrom, LocalPlayer():Team())) then
					nodude = false
				end
				
				if nodude then
					local weps = "no extra weapons"
					if #v.Weapons > 0 then
						weps = table.concat(v.Weapons, "\n")
					end
					if v.Vote then
						local condition = ((v.admin == 0 and LocalPlayer():IsAdmin()) or (v.admin == 1 and LocalPlayer():IsSuperAdmin()) or LocalPlayer().DarkRPVars["Priv"..v.command])
						if not v.model or not v.name or not v.Des or not v.command then chat.AddText(Color(255,0,0,255), "Incorrect team! Fix your shared.lua!") return end
						AddIcon(v.model, v.name, v.Des, weps, "/vote"..v.command, condition, "/"..v.command)
					else
						if not v.model or not v.name or not v.Des or not v.command then chat.AddText(Color(255,0,0,255), "Incorrect team! Fix your shared.lua!") return end
						AddIcon(v.model, v.name, v.Des, weps, "/"..v.command)
					end
				end
			end
		end
	end
	hordiv:Update()
end)

DRP.AddTab("Store",function(p)
	local EntitiesPanel = vgui.Create("DPanelList",p)
	EntitiesPanel:SetPos(ScrW()*.005,ScrH()*.007)
	EntitiesPanel:SetSize(ScrW()*.69,ScrH()*.785)
	EntitiesPanel:EnableVerticalScrollbar( true )
		function EntitiesPanel:Update()
			self:Clear(true)
			local WepCat = vgui.Create("DCollapsibleCategory")
			WepCat:SetLabel("Weapons")
				local WepPanel = vgui.Create("DPanelList")
				WepPanel:SetSize(470, 100)
				WepPanel:SetAutoSize(true)
				WepPanel:SetSpacing(1)
				WepPanel:EnableHorizontal(true)
				WepPanel:EnableVerticalScrollbar(true)
					local function AddIcon(Model, description, command, name)
						local icon = vgui.Create("SpawnIcon")
						icon:InvalidateLayout( true ) 
						icon:SetModel(Model)
						icon:SetIconSize(80)
						icon:SetToolTip(description)
						icon.DoClick = function() LocalPlayer():ConCommand("say "..command) end
						
						icon.PaintOver = function()
							draw.RoundedBox(6, 2, 2, 76, 30, Color(0,0,0,200))
							draw.SimpleTextOutlined(name[1], "Trebuchet22", 40, 15, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2, Color(0,0,0,255))
						end
						icon.PaintOverHovered = function()
							draw.RoundedBox(6, 2, 2, 76, 30, Color(0,0,0,200))
							draw.SimpleTextOutlined(name[2] or name[1], "Trebuchet22", 40, 15, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2, Color(0,0,0,255))
						end
						
						WepPanel:AddItem(icon)
					end
					
					for k,v in pairs(CustomShipments) do
						if (v.seperate and (GetConVarNumber("restrictbuypistol") == 0 or 
							(GetConVarNumber("restrictbuypistol") == 1 and (not v.allowed[1] or table.HasValue(v.allowed, LocalPlayer():Team())))))
							and (not v.customCheck or v.customCheck and v.customCheck(LocalPlayer())) then
							
							local e = string.Explode(" ",v.name)
							AddIcon(v.model, string.format(LANGUAGE.buy_a, "a "..v.name, CUR..(v.pricesep or "")), "/buy "..v.name,{e[1],e[2]})
						end
					end
					
			WepCat:SetContents(WepPanel)
			WepCat:SetSkin("DarkRP")
			self:AddItem(WepCat)
			
			local EntCat = vgui.Create("DCollapsibleCategory")
			EntCat:SetLabel("Entities")
				local EntPanel = vgui.Create("DPanelList")
				EntPanel:SetSize(470, 200)
				EntPanel:SetAutoSize(true)
				EntPanel:SetSpacing(1)
				EntPanel:EnableHorizontal(true)
				EntPanel:EnableVerticalScrollbar(true)
					local function AddEntIcon(Model, description, command, name)
						local icon = vgui.Create("SpawnIcon")
						icon:InvalidateLayout( true ) 
						icon:SetModel(Model)
						icon:SetIconSize(80)
						icon:SetToolTip(description)
						icon.DoClick = function() LocalPlayer():ConCommand("say "..command) end
						icon.PaintOver = function()
							draw.RoundedBox(6, 2, 2, 76, 30, Color(0,0,0,200))
							draw.SimpleTextOutlined(name[1], "Trebuchet22", 40, 15, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2, Color(0,0,0,255))
						end
						icon.PaintOverHovered = function()
							draw.RoundedBox(6, 2, 2, 76, 30, Color(0,0,0,200))
							draw.SimpleTextOutlined(name[2] or name[1], "Trebuchet22", 40, 15, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2, Color(0,0,0,255))
						end
						
						EntPanel:AddItem(icon)
					end
					
					for k,v in pairs(DarkRPEntities) do
						if not v.allowed or (type(v.allowed) == "table" and table.HasValue(v.allowed, LocalPlayer():Team())) 
							and (not v.customCheck or (v.customCheck and v.customCheck(LocalPlayer()))) then
							local cmdname = string.gsub(v.ent, " ", "_")
							
							if not tobool(GetConVarNumber("disable"..cmdname)) then
								local price = GetConVarNumber(cmdname.."_price")
								if price == 0 then 
									price = v.price
								end
								local e = string.Explode(" ",v.name)
								AddEntIcon(v.model, "Buy a " .. v.name .." " .. CUR .. price, v.cmd, {e[1],e[2]})
							end
						end
					end
					
					if FoodItems and (GetConVarNumber("foodspawn") ~= 0 or LocalPlayer():Team() == TEAM_COOK) and (GetConVarNumber("hungermod") == 1 or LocalPlayer():Team() == TEAM_COOK) then
						for k,v in pairs(FoodItems) do
							AddEntIcon(v.model, string.format(LANGUAGE.buy_a, "a "..k, "a few bucks"), "/buyfood "..k)
						end
					end
					for k,v in pairs(CustomShipments) do
						if not v.noship and table.HasValue(v.allowed, LocalPlayer():Team())
							and (not v.customCheck or (v.customCheck and v.customCheck(LocalPlayer()))) then
							local e = string.Explode(" ",v.name)
							AddEntIcon(v.model, string.format(LANGUAGE.buy_a, "a "..v.name .." shipment", CUR .. tostring(v.price)), "/buyshipment "..v.name,{e[1],e[2]})
						end
					end
			EntCat:SetContents(EntPanel)
			EntCat:SetSkin("DarkRP")
			self:AddItem(EntCat)
			
			
			if #CustomVehicles <= 0 then return end
			local VehicleCat = vgui.Create("DCollapsibleCategory")
			VehicleCat:SetLabel("Vehicles")
				local VehiclePanel = vgui.Create("DPanelList")
				VehiclePanel:SetSize(470, 200)
				VehiclePanel:SetAutoSize(true)
				VehiclePanel:SetSpacing(1)
				VehiclePanel:EnableHorizontal(true)
				VehiclePanel:EnableVerticalScrollbar(true)
				local function AddVehicleIcon(Model, skin, description, command)
					local icon = vgui.Create("SpawnIcon")
					icon:InvalidateLayout( true ) 
					icon:SetModel(Model)
					icon:SetSkin(skin)
					icon:SetIconSize(64)
					icon:SetToolTip(description)
					icon.DoClick = function() LocalPlayer():ConCommand("say "..command) end
					VehiclePanel:AddItem(icon)
				end
				
				local founds = 0
				for k,v in pairs(CustomVehicles) do
					if not v.allowed or table.HasValue(v.allowed, LocalPlayer():Team()) then
						local Skin = (list.Get("Vehicles")[v.name] and list.Get("Vehicles")[v.name].KeyValues and list.Get("Vehicles")[v.name].KeyValues.Skin) or "0"
						AddVehicleIcon(v.model or "models/buggy.mdl", Skin, "Buy a "..v.name.." for "..CUR..v.price, "/buyvehicle "..v.name)
						founds = founds + 1
					end
				end
			if founds ~= 0 then
				VehicleCat:SetContents(VehiclePanel)
				VehicleCat:SetSkin("DarkRP")
				self:AddItem(VehicleCat)
			else
				VehiclePanel:Remove()
				VehicleCat:Remove()
			end
		end
	EntitiesPanel:SetSkin("DarkRP")
	EntitiesPanel:Update()	
end)

local function ChatJackoolCred(um)
	chat.AddText(Color(0,255,0),"Jackool added many additions to this gamemode:\n",Color(255,255,255),
	"Economy system, Tax money system\nUpgradable money printers, Custom & redone F4 menu\nBounty (hitman) system, and more!")
	
	if um:ReadBool() then
		chat.AddText(Color(255,0,0),"Jackool is in this server right now. Say hello to him!")
	end
end
usermessage.Hook("SndJackoolCreds",ChatJackoolCred)

timer.Create("LocalPlayerIsntValidYet",6,1,function()
	if LocalPlayer():IsAdmin() then
		DRP.AddTab("Admin",function(p)
			local AdminPanel = vgui.Create("DPanelList",p)
			AdminPanel:SetPos(ScrW()*.005,ScrH()*.007)
			AdminPanel:SetSize(ScrW()*.69,ScrH()*.785)
			AdminPanel:SetSpacing(1)
			AdminPanel:EnableHorizontal( false	)
			AdminPanel:EnableVerticalScrollbar( true )
				function AdminPanel:Update()
					self:Clear(true)
					local ToggleCat = vgui.Create("DCollapsibleCategory")
					ToggleCat:SetLabel("Toggle commands")
						local TogglePanel = vgui.Create("DPanelList")
						TogglePanel:SetSize(470, 230)
						TogglePanel:SetSpacing(1)
						TogglePanel:EnableHorizontal(false)
						TogglePanel:EnableVerticalScrollbar(true)
						
						local ValueCat = vgui.Create("DCollapsibleCategory")
						ValueCat:SetLabel("Value commands")
						local ValuePanel = vgui.Create("DPanelList")
						ValuePanel:SetSize(470, 230)
						ValuePanel:SetSpacing(1)
						ValuePanel:EnableHorizontal(false)
						ValuePanel:EnableVerticalScrollbar(true)
						
						for k, v in SortedPairsByMemberValue(ToggleCmds, "var") do
							local found = false
							for a,b in pairs(HelpLabels) do
								if string.find(b.text, k) then
									found = b.text
									break
								end
							end
							if found and type(v) == "table" then
								local checkbox = vgui.Create("DCheckBoxLabel")
								checkbox:SetValue(GetConVarNumber(v.var))
								checkbox:SetText(found)
								function checkbox.Button:Toggle()
									if self:GetChecked() == nil or not self:GetChecked() then 
										self:SetValue( true ) 
									else 
										self:SetValue( false ) 
									end 
									local tonum = {}
									tonum[false] = "0"
									tonum[true] = "1"
									RunConsoleCommand(k, tonum[self:GetChecked()])
								end
								TogglePanel:AddItem(checkbox)
							end
						end
					ToggleCat:SetContents(TogglePanel)
					ToggleCat:SetSkin("DarkRP")
					self:AddItem(ToggleCat)
					function ToggleCat:Toggle()
						self:SetExpanded( !self:GetExpanded() ) 
						self.animSlide:Start( self:GetAnimTime(), { From = self:GetTall() } ) 
						if not self:GetExpanded() and ValueCat:GetExpanded() then
							ValuePanel:SetTall(470)
						elseif self:GetExpanded() and ValueCat:GetExpanded() then
							ValuePanel:SetTall(230)
							TogglePanel:SetTall(230)
						elseif self:GetExpanded() and not ValueCat:GetExpanded() then
							TogglePanel:SetTall(470)
						end 
						self:InvalidateLayout( true ) 
						self:GetParent():InvalidateLayout() 
						self:GetParent():GetParent():InvalidateLayout() 
						local cookie = '1' 
						if ( !self:GetExpanded() ) then cookie = '0' end 
						self:SetCookie( "Open", cookie )
					end  
					
					function ValueCat:Toggle()
						self:SetExpanded( !self:GetExpanded() ) 
						self.animSlide:Start( self:GetAnimTime(), { From = self:GetTall() } ) 

						if not self:GetExpanded() and ToggleCat:GetExpanded() then
							TogglePanel:SetTall(470)
						elseif self:GetExpanded() and ToggleCat:GetExpanded() then
							TogglePanel:SetTall(230)
							ValuePanel:SetTall(230)
						elseif self:GetExpanded() and not ToggleCat:GetExpanded() then
							ValuePanel:SetTall(470)
						end 
						self:InvalidateLayout( true ) 
						self:GetParent():InvalidateLayout() 
						self:GetParent():GetParent():InvalidateLayout()
						local cookie = '1' 
						if ( !self:GetExpanded() ) then cookie = '0' end 
						self:SetCookie( "Open", cookie )
					end  
						for k, v in SortedPairsByMemberValue(ValueCmds, "var") do
							local found = false
							for a,b in pairs(HelpLabels) do
								if string.find(b.text, k) then
									found = b.text
									break
								end
							end
							if found and type(v) == "table" then
								local slider = vgui.Create("DNumSlider")
								slider:SetDecimals(0)
								slider:SetMin(0)
								slider:SetMax(3000)
								slider:SetText(found)
								slider:SetValue(GetConVarNumber(v.var))
								
								function slider.Slider:OnMouseReleased()
									self:SetDragging( false ) 
									self:MouseCapture( false ) 
									RunConsoleCommand(k, slider:GetValue())
								end
								function slider.Wang:EndWang()
									self:MouseCapture( false ) 
									self.Dragging = false 
									self.HoldPos = nil 
									self.Wanger:SetCursor( "" ) 
									if ( ValidPanel( self.IndicatorT ) ) then self.IndicatorT:Remove() end 
									if ( ValidPanel( self.IndicatorB ) ) then self.IndicatorB:Remove() end 
									RunConsoleCommand(k, self:GetValue())
								end
								function slider.Wang.TextEntry:OnEnter()
									RunConsoleCommand(k, self:GetValue())
								end
								ValuePanel:AddItem(slider)
							end
						end
					ValueCat:SetContents(ValuePanel)
					ValueCat:SetSkin("DarkRP")
					self:AddItem(ValueCat)
				end
				AdminPanel:Update()
			AdminPanel:SetSkin("DarkRP")
		end)

		local DefaultWeapons = {
		{name = "GravGun",class = "weapon_physcannon"},
		{name = "Physgun",class = "weapon_physgun"},
		{name = "Crowbar",class = "weapon_crowbar"},
		{name = "Stunstick",class = "weapon_stunstick"},
		{name = "Pistol",class = "weapon_pistol"},
		{name = "357",	class = "weapon_357"},
		{name = "SMG", class = "weapon_smg1"},
		{name = "Shotgun", class = "weapon_shotgun"},
		{name = "Crossbow", class = "weapon_crossbow"},
		{name = "AR2", class = "weapon_ar2"},
		{name = "BugBait",	class = "weapon_bugbait"},
		{name = "RPG", class = "weapon_rpg"}
		}

		DRP.AddTab("Licenses",function(p)
			local weaponspanel = vgui.Create("DPanelList",p)
			weaponspanel:SetPos(ScrW()*.005,ScrH()*.007)
			weaponspanel:SetSize(ScrW()*.69,ScrH()*.785)
			weaponspanel:SetSpacing(1)
			weaponspanel:EnableHorizontal(false)
			weaponspanel:EnableVerticalScrollbar(true)
				function weaponspanel:Update()
					self:Clear(true)
					local Explanation = vgui.Create("DLabel")
					Explanation:SetText(LANGUAGE.license_tab)
					Explanation:SizeToContents()
					self:AddItem(Explanation)
					
					for k,v in pairs(DefaultWeapons) do
						if type(v) == "table" and v.name then
							local checkbox = vgui.Create("DCheckBoxLabel")
							checkbox:SetText(v.name)
							checkbox:SetValue(GetConVarNumber("licenseweapon_"..v.class))
							function checkbox.Button:Toggle()
								if ( self:GetChecked() == nil || !self:GetChecked() ) then 
									self:SetValue( true ) 
								else 
									self:SetValue( false ) 
								end 
								local tonum = {}
								tonum[false] = "0"
								tonum[true] = "1"
								RunConsoleCommand("rp_licenseweapon_".. v.class, tonum[self:GetChecked()])
							end
							self:AddItem(checkbox)
						end
					end
					
					local OtherWeps = vgui.Create("DLabel")
					OtherWeps:SetText(LANGUAGE.license_tab_other_weapons)
					OtherWeps:SizeToContents()
					self:AddItem(OtherWeps)
					for k,v in pairs(weapons.GetList()) do
						if type(v) == "table" and v.Classname then
							if v.Classname and not string.find(string.lower(v.Classname), "base") and v.Classname ~= "" then
								local checkbox = vgui.Create("DCheckBoxLabel")
								if v.PrintName then
									checkbox:SetText(v.PrintName)
								else
									checkbox:SetText(v.Classname)
								end
								checkbox:SetValue(GetConVarNumber("licenseweapon_"..v.Classname))
								function checkbox.Button:Toggle()
									if ( self:GetChecked() == nil || !self:GetChecked() ) then 
										self:SetValue( true ) 
									else 
										self:SetValue( false ) 
									end 
									local tonum = {}
									tonum[false] = "0"
									tonum[true] = "1"
									RunConsoleCommand("rp_licenseweapon_".. string.lower(v.Classname), tonum[self:GetChecked()])
								end
								self:AddItem(checkbox)
							end
						end
					end
				end
			weaponspanel:Update()
		end)
	end
end)


local cur
function DRP.ChoseHitAmount(ply)
	if LocalPlayer():Team() == TEAM_HITMAN then return end
	
	local Window = vgui.Create("DFrame")
	Window:SetSize(200,90)
	Window:SetPos(-200,ScrH()*.5-40)
	Window:MoveTo(ScrW()*.5-100,ScrH()*.5-40,.3,0)
	Window:MakePopup()
	Window:SetTitle("Bounty on " .. ply)
	Window.Paint = function()
		draw.RoundedBox(8, 0, 0, 200, 80, Color(0,0,0,240))
	end
	if cur and cur:IsValid() then cur:Close() end
	cur = Window
	Window:ShowCloseButton(false)
	
	local Scroller = vgui.Create("DNumSlider",Window)
	Scroller:SetPos(4,20)
	Scroller:SetSize(192,40)
	Scroller:SetText("How big of a bounty?")
	Scroller:SetMin(SDB_SH.MinHitPrice)
	Scroller:SetMax(SDB_SH.MaxHitPrice)
	Scroller:SetDecimals(0)
	Scroller:SetValue(SDB_SH.MinHitPrice)
	
	local p
	local function NiceClose()
		p = true Window:MoveTo(ScrW()+100,ScrH()*.5-40,.3,0) timer.Simple(.3,function() if Window:IsValid() then Window:Close() end end)
	end
	
	local Place = vgui.Create("DButton",Window)
	Place:SetPos(4,60)
	Place:SetText("Place")
	Place:SetSize(90,16)
	Place.DoClick = function() if p then return end RunConsoleCommand("_bt",ply,Scroller:GetValue()) NiceClose() end
	
	local Cancel = vgui.Create("DButton",Window)
	Cancel:SetPos(106,60)
	Cancel:SetSize(90,16)
	Cancel:SetText("Cancel")
	Cancel.DoClick = function() if p then return end NiceClose() end
end

DRP.AddTab("Bounties",function(p)
	local GroupTab = vgui.Create( "DPanel", p )
	GroupTab:SetPos(ScrW()*.005,ScrH()*.007)
	GroupTab:SetSize(ScrW()*.69,ScrH()*.785)
	local h,w = GroupTab:GetTall(),GroupTab:GetWide()
	local listx,listy = 24,h*.13
	local cur = #player.GetAll()
	
	GroupTab.Paint = function()
		draw.RoundedBox(4, 0, 0, w, h, Color(0,0,0,210))
		draw.RoundedBox(4, -20, 6, w+60, h*.09, Color(255,255,255,200))
		draw.SimpleTextOutlined("Bounties", "HUDNumber", GroupTab:GetWide()*.5, GroupTab:GetTall()*.05, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2, Color(0,0,0,255))
		
		draw.RoundedBox(8, 6, h*.09+12, w-12, h*.82, Color(255,255,255,200))
	end
	
	local txt = "Select a player above and click this to place a hit."
	local IsAss = LocalPlayer():Team() == TEAM_HITMAN
	if IsAss then txt = SDB_SH.HitmanButtonText end
	
	local PlayerList = vgui.Create( "DPanelList", p )
	local Selected
	PlayerList:SetPos(listx,listy)
	PlayerList:SetSize(w-34,h*.73)
	PlayerList:SetSpacing(4)
	PlayerList:SetPadding(4)
	
	for k,v in pairs(player.GetAll()) do
		
		local Name = v:GetName()
		local NewItem = vgui.Create("DButton")
		NewItem:SetSize(w-42,30)
		NewItem:SetText("")
		NewItem.Paint = function()
			local gw,gt = NewItem:GetWide(), NewItem:GetTall()
			draw.RoundedBox(8, 0, 0, gw, gt, (Selected == Name and Color(210,210,210,150)) or Color(190,190,190,100))
			draw.SimpleTextOutlined(Name, "Trebuchet24", gw*.01, gt*.5, Color(255,255,255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER, 2, Color(0,0,0,255))
			
			if IsAss and v.hit and v.hit > 0 then
				draw.SimpleTextOutlined("Bounty of $" .. v.hit, "Trebuchet24", gw*.7, gt*.5, Color(255,0,0), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER, 2, Color(0,0,0,255))
			end
		end
		NewItem.DoClick = function() Selected = Name end
		
		PlayerList:AddItem(NewItem)
	end
	
	local HitButton = vgui.Create( "DButton", p)
	HitButton:SetPos(listx,h*.95)
	HitButton:SetSize(w-34,h*.05)
	HitButton:SetText("")
	local on
	local clicked = function() if !Selected then return end DRP.ChoseHitAmount(string.lower(Selected)) end
	HitButton.OnCursorEntered = function() on = true end
	HitButton.OnCursorExited = function() on = false end
	HitButton.OnMousePressed = function() on = "shit" end
	HitButton.OnMouseReleased = function() clicked() on = true end

	HitButton.Paint = function()
		draw.RoundedBox(8, 0, 0, w-34, h*.05, (on == "shit" and Color(255,255,255,240)) or (on == true and Color(150,150,150,180)) or Color(255,255,255,180))
		draw.SimpleTextOutlined(txt, "Trebuchet22", (w-34)*.5, h*.05*.5, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2, Color(0,0,0,255))
	end
end)

DRP.TabCount = table.Count(DRP.Tabs)

local matBlurScreen = Material( "pp/blurscreen" ) -- Copied screenblur out of derma since I'm overriding paint
function DRP.F4Menu()
	local MN = {}
	
	MN.BG = vgui.Create("DFrame")
	MN.BG:SetSize(ScrW(),ScrH())
	MN.BG:Center()
	MN.BG:MakePopup()
	MN.BG.Paint = function()
	
		-- Stolen from derma functions:
		local Fraction = 1
 
        if ( starttime ) then
                Fraction = math.Clamp( (SysTime() - starttime) / 1, 0, 1 )
        end
 
        local x, y = MN.BG:LocalToScreen( 0, 0 )
 
        DisableClipping( true )
       
        surface.SetMaterial( matBlurScreen )   
        surface.SetDrawColor( 255, 255, 255, 255 )
               
        for i=0.33, 1, 0.33 do
                matBlurScreen:SetMaterialFloat( "$blur", Fraction * 5 * i )
                if ( render ) then render.UpdateScreenEffectTexture() end
                surface.DrawTexturedRect( x * -1, y * -1, ScrW(), ScrH() )
        end
       
        surface.SetDrawColor( 10, 10, 10, 200 * Fraction )
        surface.DrawRect( x * -1, y * -1, ScrW(), ScrH() )
       
        DisableClipping( false )
		------------------------------------------ My code below
		
		draw.RoundedBox( 0, 0, 0, ScrW(), ScrH(), Color( 0, 0, 0, 140 ) )
		draw.RoundedBox( 0, ScrW()*.04-5, 0, 110, ScrH(), Color( 37, 37, 37, 150 ) )
		draw.RoundedBox( 0, ScrW()*.04, 0, 100, ScrH(), Color( 150, 150, 150, 60 ) )
		draw.SimpleTextOutlined( SDB_SH.MenuText, "Trebuchet22", ScrW()*.99, ScrH()*.99, SDB_SH.MenuTextClr, TEXT_ALIGN_RIGHT, TEXT_ALIGN_BOTTOM, 2, Color(0,0,0,195) )
	end
	
	local AllowClose = CurTime() + 1

	MN.BG:SetDraggable(false)
	MN.BG:SetTitle("")
	MN.BG.Think = function()
		if input.IsKeyDown(KEY_ESCAPE) or (input.IsKeyDown(KEY_F4) and CurTime() > AllowClose) then
			MN.BG:Close()
		end
	end

	local function DoReset()
		MN.CurMenu = vgui.Create("DPanelList",MN.BG)
		local w,h = ScrW()*.7,ScrH()*.8
		MN.CurMenu:SetPos(ScrW()*.2,ScrH()*.1)
		MN.CurMenu:SetSize(w+1,h)
		MN.CurMenu:SetPadding(4)
		MN.CurMenu:SetSpacing(4)
		MN.CurMenu.Paint = function()
			draw.RoundedBox( 6, 0, 0, w, h, Color( 57, 57, 57, 250 ) )
			draw.RoundedBox( 6, 4, 4, w-8, h-8, Color( 110, 110, 110, 150 ) )
		end
	end
	MN.PreventDupe = ""
	DoReset()
	DRP.Tabs["Commands"].derma(MN.CurMenu)
	
	local Count = 0
	for k,v in pairs(DRP.Tabs) do
		Count = Count+1
		
		local TabButton = vgui.Create("DButton",MN.BG)
		TabButton:SetPos(ScrW()*.0475-4,(ScrH()*.1*(Count)))
		local Hovered,Pressed = false
		TabButton.Paint = function()
			draw.SimpleTextOutlined(k,(Pressed and "Trebuchet22") or (Hovered and "Trebuchet24") or "Trebuchet22",44,10,((Pressed or Hovered) and Color(190,190,190,200)) or Color(255,255,255,200),TEXT_ALIGN_CENTER,TEXT_ALIGN_CENTER, 2, Color(0,0,0,200) )
		end
		TabButton.OnMousePressed = function() Pressed = true end
		TabButton.OnMouseReleased = function()
			LocalPlayer():EmitSound("ui/buttonclick.wav")
			Pressed = false
			if MN.PreventDupe == k then return end
			
			MN.PreventDupe = k
			MN.CurMenu:Remove()
			DoReset()
			
			v.derma(MN.CurMenu)
		end
		TabButton.OnCursorEntered = function() LocalPlayer():EmitSound("ui/buttonrollover.wav") Hovered = true end
		TabButton.OnCursorExited = function() Hovered = false Pressed = false end
		TabButton:SetSize(88,20)
		TabButton:SetText("")
	end
	
end
usermessage.Hook("ChangeJobVGUI", DRP.F4Menu)

local KeyFrameVisible = false
local function KeysMenu(um)
	local Vehicle = um:ReadBool()
	if KeyFrameVisible then return end
	local trace = LocalPlayer():GetEyeTrace()
	local Frame = vgui.Create("DFrame")
	KeyFrameVisible = true
	Frame:SetSize(200, 470)
	Frame:Center()
	Frame:SetVisible(true)
	Frame:MakePopup()
	
	function Frame:Think()
		local ent = LocalPlayer():GetEyeTrace().Entity
		if not ValidEntity(ent) or (not ent:IsDoor() and not string.find(ent:GetClass(), "vehicle")) or ent:GetPos():Distance(LocalPlayer():GetPos()) > 200 then
			self:Close()
		end
		if (!self.Dragging) then return end  
		local x = gui.MouseX() - self.Dragging[1] 
		local y = gui.MouseY() - self.Dragging[2] 
		x = math.Clamp( x, 0, ScrW() - self:GetWide() ) 
		y = math.Clamp( y, 0, ScrH() - self:GetTall() ) 
		self:SetPos( x, y ) 
	end
	local Entiteh = "door"
	if Vehicle then
		Entiteh = "vehicle"
	end
	Frame:SetTitle(Entiteh .. " options")
	
	function Frame:Close()
		KeyFrameVisible = false
		self:SetVisible( false )
		self:Remove()
	end
	
	if trace.Entity:OwnedBy(LocalPlayer()) then
		if not trace.Entity.DoorData then return end -- Don't open the menu when the door settings are not loaded yet
		local Owndoor = vgui.Create("DButton", Frame)
		Owndoor:SetPos(10, 30)
		Owndoor:SetSize(180, 100)
		Owndoor:SetText("Sell " .. Entiteh)
		Owndoor.DoClick = function() RunConsoleCommand("say", "/toggleown") Frame:Close() end
		
		local AddOwner = vgui.Create("DButton", Frame)
		AddOwner:SetPos(10, 140)
		AddOwner:SetSize(180, 100)
		AddOwner:SetText("Add owner")			
		AddOwner.DoClick = function()
			local menu = DermaMenu()
			for k,v in pairs(player.GetAll()) do
				if not trace.Entity:OwnedBy(v) and not trace.Entity:AllowedToOwn(v) then
					menu:AddOption(v:Nick(), function() LocalPlayer():ConCommand("say /ao ".. v:UserID()) end)
				end
			end
			if #menu.Items == 0 then
				menu:AddOption("Noone available", function() end)
			end
			menu:Open()
		end
		
		local RemoveOwner = vgui.Create("DButton", Frame)
		RemoveOwner:SetPos(10, 250)
		RemoveOwner:SetSize(180, 100)
		RemoveOwner:SetText("Remove owner")			
		RemoveOwner.DoClick = function()
			local menu = DermaMenu()
			for k,v in pairs(player.GetAll()) do
				if (trace.Entity:OwnedBy(v) and not trace.Entity:IsMasterOwner(v)) or trace.Entity:AllowedToOwn(v) then
					menu:AddOption(v:Nick(), function() LocalPlayer():ConCommand("say /ro ".. v:UserID()) end)
				end
			end
			if #menu.Items == 0 then
				menu:AddOption("Noone available", function() end)
			end
			menu:Open()
		end
		
		local DoorTitle = vgui.Create("DButton", Frame)
		DoorTitle:SetPos(10, 360)
		DoorTitle:SetSize(180, 100)
		DoorTitle:SetText("Set "..Entiteh.." title")
		if not trace.Entity:IsMasterOwner(LocalPlayer()) then
			RemoveOwner.m_bDisabled = true
		end
		DoorTitle.DoClick = function()
			Derma_StringRequest("Set door title", "Set the title of the "..Entiteh.." you're looking at", "", function(text) LocalPlayer():ConCommand("say /title ".. text) Frame:Close() end, function() end, "OK!", "CANCEL!")
		end
		
		if LocalPlayer():IsSuperAdmin() and not Vehicle then
			Frame:SetSize(200, Frame:GetTall() + 110)
			local SetCopsOnly = vgui.Create("DButton", Frame)
			SetCopsOnly:SetPos(10, Frame:GetTall() - 110)
			SetCopsOnly:SetSize(180, 100)
			SetCopsOnly:SetText("Edit Door Group")
			SetCopsOnly.DoClick = function() 
				local menu = DermaMenu()
				local groups = menu:AddSubMenu("Door Groups")
				local teams = menu:AddSubMenu("Jobs")
				local add = teams:AddSubMenu("Add")
				local remove = teams:AddSubMenu("Remove")
				menu:AddOption("None", function() RunConsoleCommand("say", "/togglegroupownable") Frame:Close() end)
				for k,v in pairs(RPExtraTeamDoors) do
					groups:AddOption(k, function() RunConsoleCommand("say", "/togglegroupownable "..k) Frame:Close() end)
				end
				if not trace.Entity.DoorData then return end
				for k,v in pairs(RPExtraTeams) do
					if not trace.Entity.DoorData.TeamOwn or not trace.Entity.DoorData.TeamOwn[k] then
						add:AddOption( v.name, function() RunConsoleCommand("say", "/toggleteamownable " ..k) Frame:Close() end )
					else
						remove:AddOption( v.name, function() RunConsoleCommand("say", "/toggleteamownable " ..k) Frame:Close() end )
					end
				end
				
				menu:Open()
			end
		end	
	elseif not trace.Entity:OwnedBy(LocalPlayer()) and trace.Entity:IsOwnable() and not trace.Entity:IsOwned() and not trace.Entity.DoorData.NonOwnable then
		if not trace.Entity.DoorData.GroupOwn then
			Frame:SetSize(200, 140)
			local Owndoor = vgui.Create("DButton", Frame)
			Owndoor:SetPos(10, 30)
			Owndoor:SetSize(180, 100)
			Owndoor:SetText("Buy " .. Entiteh)
			Owndoor.DoClick = function() RunConsoleCommand("say", "/toggleown") Frame:Close() end
		elseif not LocalPlayer():IsSuperAdmin() then
			Frame:Close()
		end
		if LocalPlayer():IsSuperAdmin() then
			if trace.Entity.DoorData.GroupOwn then
				Frame:SetSize(200, 250)
			else
				Frame:SetSize(200, 360)
			end
			local DisableOwnage = vgui.Create("DButton", Frame)
			DisableOwnage:SetPos(10, Frame:GetTall() - 220)
			DisableOwnage:SetSize(180, 100)
			DisableOwnage:SetText("Disallow ownership")
			DisableOwnage.DoClick = function() Frame:Close() RunConsoleCommand("say", "/toggleownable") end
			
			local SetCopsOnly = vgui.Create("DButton", Frame)
			SetCopsOnly:SetPos(10, Frame:GetTall() - 110)
			SetCopsOnly:SetSize(180, 100)
			SetCopsOnly:SetText("Edit Door Group")
			SetCopsOnly.DoClick = function() 
				local menu = DermaMenu()
				local groups = menu:AddSubMenu("Door Groups")
				local teams = menu:AddSubMenu("Jobs")
				local add = teams:AddSubMenu("Add")
				local remove = teams:AddSubMenu("Remove")
				menu:AddOption("None", function() RunConsoleCommand("say", "/togglegroupownable") Frame:Close() end)
				for k,v in pairs(RPExtraTeamDoors) do
					groups:AddOption(k, function() RunConsoleCommand("say", "/togglegroupownable "..k) Frame:Close() end)
				end
				if not trace.Entity.DoorData then return end
				for k,v in pairs(RPExtraTeams) do
					if not trace.Entity.DoorData.TeamOwn or not trace.Entity.DoorData.TeamOwn[k] then
						add:AddOption( v.name, function() RunConsoleCommand("say", "/toggleteamownable " ..k) if Frame.Close then Frame:Close() end end )
					else
						remove:AddOption( v.name, function() RunConsoleCommand("say", "/toggleteamownable " ..k) Frame:Close() end )
					end
				end
				
				menu:Open()
			end
		elseif not trace.Entity.DoorData.GroupOwn then
			RunConsoleCommand("say", "/toggleown")
			Frame:Close()
			KeyFrameVisible = true
			timer.Simple(0.3, function() KeyFrameVisible = false end)
		end
	elseif not trace.Entity:OwnedBy(LocalPlayer()) and trace.Entity:AllowedToOwn(LocalPlayer()) then
		Frame:SetSize(200, 140)
		local Owndoor = vgui.Create("DButton", Frame)
		Owndoor:SetPos(10, 30)
		Owndoor:SetSize(180, 100)
		Owndoor:SetText("Co-own " .. Entiteh)
		Owndoor.DoClick = function() RunConsoleCommand("say", "/toggleown") Frame:Close() end
		
		if LocalPlayer():IsSuperAdmin() then
			Frame:SetSize(200, Frame:GetTall() + 110)
			local SetCopsOnly = vgui.Create("DButton", Frame)
			SetCopsOnly:SetPos(10, Frame:GetTall() - 110)
			SetCopsOnly:SetSize(180, 100)
			SetCopsOnly:SetText("Edit Door Group")
			SetCopsOnly.DoClick = function() 
				local menu = DermaMenu()
				local groups = menu:AddSubMenu("Door Groups")
				local teams = menu:AddSubMenu("Jobs")
				local add = teams:AddSubMenu("Add")
				local remove = teams:AddSubMenu("Remove")
				menu:AddOption("None", function() RunConsoleCommand("say", "/togglegroupownable") Frame:Close() end)
				for k,v in pairs(RPExtraTeamDoors) do
					groups:AddOption(k, function() RunConsoleCommand("say", "/togglegroupownable "..k) Frame:Close() end)
				end
				if not trace.Entity.DoorData then return end
				for k,v in pairs(RPExtraTeams) do
					if not trace.Entity.DoorData.TeamOwn or not trace.Entity.DoorData.TeamOwn[k] then
						add:AddOption( v.name, function() RunConsoleCommand("say", "/toggleteamownable " ..k) Frame:Close() end )
					else
						remove:AddOption( v.name, function() RunConsoleCommand("say", "/toggleteamownable " ..k) Frame:Close() end )
					end
				end
				
				menu:Open()
			end
		else
			RunConsoleCommand("say", "/toggleown")
			Frame:Close()
			KeyFrameVisible = true
			timer.Simple(0.3, function() KeyFrameVisible = false end)
		end	
	elseif LocalPlayer():IsSuperAdmin() and trace.Entity.DoorData.NonOwnable then
		Frame:SetSize(200, 250)
		local EnableOwnage = vgui.Create("DButton", Frame)
		EnableOwnage:SetPos(10, 30)
		EnableOwnage:SetSize(180, 100)
		EnableOwnage:SetText("Allow ownership")
		EnableOwnage.DoClick = function() Frame:Close() RunConsoleCommand("say", "/toggleownable") end
		
		local DoorTitle = vgui.Create("DButton", Frame)
		DoorTitle:SetPos(10, Frame:GetTall() - 110)
		DoorTitle:SetSize(180, 100)
		DoorTitle:SetText("Set "..Entiteh.." title")
		DoorTitle.DoClick = function()
			Derma_StringRequest("Set door title", "Set the title of the "..Entiteh.." you're looking at", "", function(text) LocalPlayer():ConCommand("say /title ".. text) Frame:Close() end, function() end, "OK!", "CANCEL!")
		end
	elseif LocalPlayer():IsSuperAdmin() and not trace.Entity:OwnedBy(LocalPlayer()) and trace.Entity:IsOwned() and not trace.Entity:AllowedToOwn(LocalPlayer()) then
		Frame:SetSize(200, 250)
		local DisableOwnage = vgui.Create("DButton", Frame)
		DisableOwnage:SetPos(10, 30)
		DisableOwnage:SetSize(180, 100)
		DisableOwnage:SetText("Disallow ownership")
		DisableOwnage.DoClick = function() Frame:Close() RunConsoleCommand("say", "/toggleownable") end
		
		local SetCopsOnly = vgui.Create("DButton", Frame)
		SetCopsOnly:SetPos(10, Frame:GetTall() - 110)
		SetCopsOnly:SetSize(180, 100)
		SetCopsOnly:SetText("Edit Door Group")
			SetCopsOnly.DoClick = function() 
				local menu = DermaMenu()
				local groups = menu:AddSubMenu("Door Groups")
				local teams = menu:AddSubMenu("Jobs")
				local add = teams:AddSubMenu("Add")
				local remove = teams:AddSubMenu("Remove")
				menu:AddOption("None", function() RunConsoleCommand("say", "/togglegroupownable") Frame:Close() end)
				for k,v in pairs(RPExtraTeamDoors) do
					groups:AddOption(k, function() RunConsoleCommand("say", "/togglegroupownable "..k) Frame:Close() end)
				end
				if not trace.Entity.DoorData then return end
				for k,v in pairs(RPExtraTeams) do
					if not trace.Entity.DoorData.TeamOwn or not trace.Entity.DoorData.TeamOwn[k] then
						add:AddOption( v.name, function() RunConsoleCommand("say", "/toggleteamownable " ..k) Frame:Close() end )
					else
						remove:AddOption( v.name, function() RunConsoleCommand("say", "/toggleteamownable " ..k) Frame:Close() end )
					end
				end
				
				menu:Open()
			end
	else
		Frame:Close()
	end
	
	Frame:SetSkin("DarkRP")
end
usermessage.Hook("KeysMenu", KeysMenu)

--Begin Client-side trade system - By Eusion.
local TradeMenus = {}
local function TradeMenuClient(handler, id, encoded, decoded)
	local items = decoded
	
	local TradeFrame = vgui.Create("DFrame")
	TradeFrame:SetSize((#items * 64)+20, 110)
	TradeFrame:Center()
	TradeFrame:SetTitle("Initialize a trade")
	TradeFrame:MakePopup()
	TradeFrame:SetSkin("DarkRP")
	
	local ItemsForm = vgui.Create("DPanelList", TradeFrame)
	ItemsForm:SetSize((#items * 64), 64)
	ItemsForm:SetPos(10, 31)
	ItemsForm:SetSpacing(0)
	ItemsForm:EnableHorizontal(true)
	ItemsForm:EnableVerticalScrollbar(true)
	
	for k, v in pairs(items) do
		if ValidEntity(v) then
			local k = vgui.Create("SpawnIcon")
			k:SetModel(v:GetModel())
			k.DoClick = function()
				LocalPlayer():ConCommand("rp_tradeitem " .. v:EntIndex())
				TradeFrame:Close()
				CloseDermaMenus()
			end
			k:SetToolTip(v:GetClass())
			k:SetIconSize(64)
			ItemsForm:AddItem(k)
		end
	end
end
--  datastream.Hook("darkrp_trade", TradeMenuClient)

local function TradeMenuRecipient(um)
	local client = um:ReadEntity()
	local recipient = um:ReadEntity()
	local trade = um:ReadEntity()
	local id = um:ReadShort()
	
	if not ValidEntity(client) then return end
	if not ValidEntity(recipient) then return end
	if not ValidEntity(trade) then return end

	local TradeFrame = vgui.Create("DFrame")
	TradeFrame:SetSize(ScrW()/4, 250)
	TradeFrame:Center()
	TradeFrame:SetTitle("Trade interface")
	TradeFrame:MakePopup()
	TradeFrame:SetSkin("DarkRP")
	function TradeFrame:Close()
		LocalPlayer():ConCommand("rp_killtrade " .. id)
		self:Remove()
	end
	
	local ItemsForm = vgui.Create("DPanel", TradeFrame)
	ItemsForm:SetSize((TradeFrame:GetWide())-20, 209)
	ItemsForm:SetPos(10, 31)
	ItemsForm.Paint = function()
		surface.DrawLine(ItemsForm:GetWide()/2, 21, (ItemsForm:GetWide()/2), 209)
	end
	
	local ClientLabel = vgui.Create("DLabel", ItemsForm)
	ClientLabel:SetText("Trade: " .. client:Name())
	ClientLabel:SetPos(5, 5)
	ClientLabel:SizeToContents()
	
	local RecipientLabel = vgui.Create("DLabel", ItemsForm)
	RecipientLabel:SetText("Trade: " .. recipient:Name())
	RecipientLabel:SetPos((ItemsForm:GetWide()/2)+15, 5)
	RecipientLabel:SizeToContents()
	
	local TradeClient = vgui.Create("SpawnIcon", ItemsForm)
	TradeClient:SetModel(trade:GetModel())
	TradeClient:SetToolTip(trade:GetClass())
	TradeClient:SetPos(5, 10+ClientLabel:GetTall())
	
	TradeRecipient = vgui.Create("SpawnIcon", ItemsForm)
	TradeRecipient:SetModel(trade:GetModel())
	TradeRecipient:SetToolTip(trade:GetClass())
	TradeRecipient:SetPos((ItemsForm:GetWide()/2)+15, 10+RecipientLabel:GetTall())
	
	if LocalPlayer() == recipient then
		
	end
end
usermessage.Hook("darkrp_trade", TradeMenuRecipient)

local function TradeRequest(um)
	local id = um:ReadShort()
	local client = um:ReadEntity()
	local trade = um:ReadEntity()

	LocalPlayer():EmitSound("Town.d1_town_02_elevbell1", 100, 100)
	local panel = vgui.Create("DFrame")
	panel:SetPos(3 + PanelNum, ScrH() / 2 - 50)
	panel:SetTitle("Trade")
	panel:SetSize(140, 140)
	panel:SetSizable(false)
	panel.btnClose:SetVisible(false)
	panel:SetDraggable(false)
	function panel:Close()
		PanelNum = PanelNum - 140
		VoteVGUI[id .. "_trade"] = nil
		
		local num = 0
		for k,v in SortedPairs(VoteVGUI) do
			v:SetPos(num, ScrH() / 2 - 50)
			num = num + 140
		end
		
		for k,v in SortedPairs(QuestionVGUI) do
			v:SetPos(num, ScrH() / 2 - 50)
			num = num + 300
		end
		
		LocalPlayer():ConCommand("rp_killtrade " .. id)
		self:Remove()
	end

	panel:SetKeyboardInputEnabled(false)
	panel:SetMouseInputEnabled(true)
	panel:SetVisible(true)

	local label = vgui.Create("Label")
	label:SetParent(panel)
	label:SetPos(5, 30)
	label:SetSize(180, 40)
	label:SetText(client:Name() .. "\nWants to trade:\n" .. trade:GetClass())
	label:SetVisible(true)

	local divider = vgui.Create("Divider")
	divider:SetParent(panel)
	divider:SetPos(2, 80)
	divider:SetSize(180, 2)
	divider:SetVisible(true)

	local ybutton = vgui.Create("Button")
	ybutton:SetParent(panel)
	ybutton:SetPos(25, 100)
	ybutton:SetSize(40, 20)
	ybutton:SetCommand("!")
	ybutton:SetText("Yes")
	ybutton:SetVisible(true)
	ybutton.DoClick = function()
		LocalPlayer():ConCommand("rp_tradevote " .. id .. " yes")
		panel:Close()
	end

	local nbutton = vgui.Create("Button")
	nbutton:SetParent(panel)
	nbutton:SetPos(70, 100)
	nbutton:SetSize(40, 20)
	nbutton:SetCommand("!")
	nbutton:SetText("No")
	nbutton:SetVisible(true)
	nbutton.DoClick = function()
		LocalPlayer():ConCommand("rp_tradevote " .. id .. " no")
		panel:Close()
	end

	PanelNum = PanelNum + 140
	VoteVGUI[id .. "_trade"] = panel
	panel:SetSkin("DarkRP")
	
	timer.Simple(20, function()
		LocalPlayer():ConCommand("rp_tradevote " .. id .. " no")
		panel:Close()
	end)
end
usermessage.Hook("darkrp_treq", TradeRequest)

local function KillTrade(um)
	
end
usermessage.Hook("darkrp_killtrade", KillTrade)